#include "mutex_demo.h"

TSK_INIT_PARAM_S task1;
TSK_INIT_PARAM_S task2;

static VOID TaskF01(void)
{
    UINT32 ret;
    printf("\n++++++++Yang Zihao's TaskF01: (1) before MUX pend success, check actual Count = %d, it should be 2\n", Count);
    ret = LOS_MuxPend(g_mutexTest1, LOS_WAIT_FOREVER);
    Count++;
    // LOS_TaskDelay(50);
    ret = LOS_MuxPost(g_mutexTest1);
    printf("\n++++++++Yang Zihao's TaskF01: (2) before MUX post success, check actual Count = %d, it should be 3\n", Count);
    return;
}

static VOID TaskF02(void)
{
    UINT32 ret;
    Count++;
    printf("\n++++++++Yang Zihao's TaskF02: (1) before MuxPend check actual Count = %d, it should be 1\n", Count);
    ret = LOS_MuxPend(g_mutexTest1, LOS_WAIT_FOREVER);
    Count++;
    printf("\n++++++++Yang Zihao's TaskF02: (2) after MuxPend (before MuxPost) check actual Count = %d, it should be 2\n", Count);
    printf("\n++++++++Yang Zihao's TaskF02: (3) continue hold mutex for 50 ticks, TaskF01 should be scheduled\n");
    LOS_TaskDelay(50);
    printf("\n++++++++Yang Zihao's TaskF02: (4) mux post, TaskF01 will continue exe within the next 50 ticks\n");
    ret = LOS_MuxPost(g_mutexTest1);
    LOS_TaskDelay(10);
    Count++;
    printf("\n++++++++Yang Zihao's TaskF02: (5) after MuxPost check actual Count = %d, it should be 4\n", Count);
}

VOID  Mutex_Demo(VOID)
{
    UINT32 ret;
    ret = LOS_MuxCreate(&g_mutexTest1);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Mutex_Demo: mutex created ok with handle = 0x%x\n", Count);
    
    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
    task1.usTaskPrio = TASK_PRIO_TEST;
    task1.pcName = "VMuteB2_1";
    task1.uwStackSize = 0x900;
    task1.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID01, &task1);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Mutex_Demo: TaskF01 created ok with ID = 0x%x\n", g_testTaskID01);

    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
    task2.usTaskPrio = (TASK_PRIO_TEST - 2); 
    task2.pcName = "VMuteB2_2";
    task2.uwStackSize = 0x900;
    task2.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID02, &task2);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Mutex_Demo: TaskF02 created ok with ID = 0x%x\n", g_testTaskID02);

    printf("\n++++++++Yang Zihao's Mutex_Demo: will delay 90 ticks\n");
    LOS_TaskDelay(90);

    printf("\n++++++++Yang Zihao's Mutex_Demo: after 90 ticks, finally Count = %d\n", Count);

    ret = LOS_MuxDelete(g_mutexTest1);
    if(ret == LOS_OK)
        printf("\n++++++++Yang Zihao's Mutex_Demo: g_mutexTest1 deleted ok\n");

    return; 
}


